package de.gwtprime.client.widgets;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.AnswerOnTypeEvent;
import de.gwtprime.client.event.ApplyTabHeightEvent;
import de.gwtprime.client.event.handler.AnswerOnTypeEventHandler;
import de.gwtprime.client.presenter.VoteControlPresenterAnswerImpl;
import de.gwtprime.client.user.ACLManager;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.shared.requestFactory.AnswerProxy;
import de.gwtprime.shared.requestFactory.QuestionProxy;

public class AnswerWidget extends Composite{

	interface AnswerWidgetUiBinder extends UiBinder<Widget, AnswerWidget>{}
	AnswerWidgetUiBinder uiBinder = GWT.create(AnswerWidgetUiBinder.class);

	@UiField HTML text;
	
	@UiField HTML postedByLbl;
	@UiField Label userName;
	@UiField SimplePanel voteControl;

	@UiField Button editBtn;

	@UiField SimplePanel editAnswerWidget;
	
	boolean isPreview = false;

	public AnswerWidget(final ClientFactory clientFactory, final AnswerProxy answer, QuestionProxy question) {
		initWidget(uiBinder.createAndBindUi(this));

		isPreview = answer == null;
		postedByLbl.setHTML("Author:&nbsp;");
		if(answer!=null){
			text.setHTML(AppUtils.editForClient(answer.getText(), answer.getImages()));
			userName.setText(answer.getUser() == null ? "-" : answer.getUser().getName());
		}
		
		if(!isPreview){
			// control widget
			ArticleSectionControlWidget controlWidget = new ArticleSectionControlWidget(clientFactory);
			VoteControlPresenterAnswerImpl controlPresenter = new VoteControlPresenterAnswerImpl(answer, question, clientFactory, controlWidget);
			controlWidget.initWidget(controlPresenter);
			voteControl.add(controlWidget);
			
		}else{
			addStyleName("isPreview");
		}
		postedByLbl.setVisible(!isPreview);
		
		editBtn.setVisible(!isPreview 
				&& ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.AnswerAdd)
				&& answer.getUser() != null && clientFactory.getUser().equals(answer.getUser()));

		editBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				editAnswerWidget.clear();
				editAnswerWidget.add(new AddAnswerWidget(null, clientFactory, answer));

			}
		});


		clientFactory.getEventBus().addHandler(AnswerOnTypeEvent.TYPE, new AnswerOnTypeEventHandler() {

			@Override
			public void onType(AnswerOnTypeEvent event) {

				if((event.getId()==null && (answer == null ||answer.getId() ==null) ) || (answer!=null && answer.getId().equals(event.getId()))){
					text.setHTML(AppUtils.editForClient(event.getText(), event.getImages()));
					clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent());
				}

			}
		});

	}

}
